//init 
$(function(){
	role.init();
	role.displayTable();
	role.displayTree();

   
//	$(selector).mCustomScrollbar({ option: value });
//	role.initEditTalbe();
});
//namespace
var role= {};
role.treeresult=[];
role.rid = null;
role.opersid = [];
role.index=null;
role.index2=null;
role.editId=null;
role.editremoveaids=[];
role.id=null;
role.init=function (){
	 $("#authtreeScroll").mCustomScrollbar();
	 $("#listscroll").mCustomScrollbar();
	var aaa=  $('#nonitembased');
	var bbb = $('#listscroll');
	var ccc=$('#authtreeScroll');
	bbb.perfectScrollbar();
	var headerHeight =$("#header").height();
	var footerHeight =$("#footer").height();
	var bcHeight =$("#breadcrumbDiv").height();
	var windowHeight = $(window).height(); 	
	var hhh = windowHeight - bcHeight - footerHeight - headerHeight - 300;
	var nh = 60//$(".content-wrap");
	bbb.css("height",  hhh+ "px");
	aaa.css("height",  hhh+ "px");
	ccc.css("height",  hhh+ "px");
	$("#save").attr("disabled", true);
}
role.displayTable=function () {	
	var url = "sys/findAllRoles";
	var data = {};
	var successCallBack = function(data) {
		var result = data.result;
		var size = result.length;
		for (var i = 0; i < size; i++) {
			var rolename = result[i].roleName;
			var roleid = result[i].roleId;
			$("#rolelist").append("<li id='"+roleid+"' href='javascript:void(0)' " +
					"onclick=role.displayAuth("+roleid+") class='list-group-item a'>"+rolename+"</li>");
		}
	};

	$.sysAjax(url, data, successCallBack);
}

role.displayTree=function () {
	
	sysShowGlobalLoading();
	var url = "sys/findAllOpers";
	var data = {

	};
	var successCallBack = function(data) {
		if (data.result) {
			
		} else {

		}
		var opers = data.result;
		for ( var i in opers) {
			var tmpOper = opers[i];
			aaa = sysi18n[tmpOper.text];
			if( aaa != null ){
				tmpOper.text = aaa;
			}
		}
		role.treeresult=opers;
		$("#authtree").jstree({
			'core' : {
				'data' : role.treeresult,
			},
			"checkbox" : {
				"keep_selected_style" : false,
//				"tie_selection":false,
//				"three_state":false,
//				"cascade":"up",
			},
			"plugins" : [ "checkbox"],
		}).bind("loaded.jstree", function(e, data) {
			data.instance.open_all();	
			for(var i=0;i<role.treeresult.length;i++){
				if(data.instance.is_leaf(role.treeresult[i])){
					data.instance.set_icon(role.treeresult[i],"glyphicon glyphicon-leaf");
				}
				else{
					data.instance.set_icon(role.treeresult[i],"glyphicon glyphicon-flash");
				}
			}
		});
		$('#authtree').on("changed.jstree", function(e, data) {
			var i, j, r = [],pid,intpid;
			for (i = 0, j = data.selected.length; i < j; i++) {
				var node = data.instance.get_node(data.selected[i]);  
				intpid =parseInt(node.id);;
				var ids = data.instance.get_path(node,"/",true);
				r.push(ids);

			}
			role.opersid = r;
		});
	};
	$.sysAjax(url, data, successCallBack);
	
}
role.displayAuth=function (id) {
	role.id=id;
	$("#save").attr("disabled", false);
	document.getElementById("selectedrole").innerHTML =sysi18n.Selected +$("#rolelist li[id='"+id+"']").html();
	$("#rolelist li").attr("class","list-group-item");
	$("#rolelist li[id='"+id+"']").attr("class","list-group-item active");
	sysShowGlobalLoading();
	var url = "sys/findAuthByRole";
	role.rid = id;
	var data = {
		rid : id,

	};
	var successCallBack = function(data) {
		if (data.result) {
		} else {
		}
		$('#authtree').jstree('uncheck_all');
		
		
		var result = data.result;
		var size = result.length;
		for (var i = 0; i < size; i++) {
			var node = $("#authtree").jstree("get_node", data.result[i]);
			if ($("#authtree").jstree("is_leaf", data.result[i])) {  
            	$("#authtree").jstree("check_node", data.result[i]);
            }  
			
		}
		

	}
	$.sysAjax(url, data, successCallBack);
}
role.save=function () {
	var rname=$("#rolelist li[id='"+role.id+"']").text();
		sysShowGlobalLoading();
		var url = "sys/updGrantOperToRole";
		var data = {
			roleId : role.rid,
			"opers[]" : role.opersid,
			rname:rname,

		};
		var successCallBack = function(data) {
			if (data.result) {
				sysShowSuccessTip(sysi18n.Save_success);
			} else {
				sysShowTip(sysi18n.Save_failed);
			}
		}
		$.sysAjax(url, data, successCallBack);
	
}
role.openAll=function()  {
	$('#authtree').jstree('open_all');
}
role.closeAll=function () {
	$('#authtree').jstree('close_all');
}
role.addRole=function (){
	
	role.index= layer.open({
	    	  type: 1,
	    	  title: sysi18n.Addrole,
	    	  shadeClose: true,
	    	  shade: 0,
	    	  zIndex : 2,
	    	  move: false,
	    	  area: ['400px', '200px'],
	    	  content:$('#addroleform')
	    	  
		});

}
role.saveRole=function(){
	
	if($("#newRolename").val()!=""){
	sysShowGlobalLoading();
	var url  = "sys/addsysRole";
	var data = {
			rolename:$("#newRolename").val(),
			roleremark:$("#newRoleremark").val(),
	};
	var successCallBack = function(data) {
		if (data.result) {
			var rolename = data.result[0].roleName;
			var roleid = data.result[0].roleId;
			$("#rolelist").append("<li id='"+roleid+"' href='javascript:void(0)' " +
					"onclick=role.displayAuth("+roleid+") class='list-group-item'>"+rolename+"</li>");
			layer.close();
			sysShowSuccessTip(sysi18n.Add_success);
			 parent.layer.close(role.index);
		} else {
			sysShowTip(sysi18n.Saverole_failed);
		}
	}
	$.sysAjax(url, data, successCallBack);
	}
	else{
		sysShowTip(sysi18n.Null_role);
	}
   }
role.search=function() {
	var intext=$("#input").val();
	$('#rolelist li').each(function(){
       
            var val = $(this).html();
            if((val.toLowerCase()).indexOf(intext.toLowerCase())!=-1)
            	$(this).show();
            else
            	$(this).hide();
           
    });
}

role.deleteRole= function(){
	var rname=$("#rolelist li[id='"+role.id+"']").text();
	yesCallBack=function(){
		sysShowGlobalLoading();
		var url  = "sys/deletesysRole";
		var data = {
				"rid":role.id,
				"rname":rname,
		};
		var successCallBack = function(data) {
			if(data.result){
				if("isAdmin"!=data.result){
					sysShowSuccessTip(sysi18n.Delete_success);
					$("#rolelist li[id='"+role.id+"']").remove();
					role.id=null;
					document.getElementById("selectedrole").innerHTML =sysi18n.Selected +sysi18n.Null;
					$('#authtree').jstree('uncheck_all');
					$("#save").attr("disabled", true);
					}
				else
					sysShowTip(sysi18n.isAdmin);	
			}
			else
				sysShowTip(sysi18n.Delete_failed);	
	
		}
		$.sysAjax(url, data, successCallBack);
		}
	noCallBack=function(){return false;}
	if(role.id!=null){
		sysShowConfirm(yesCallBack,noCallBack,sysi18n.IsDelete);
		
		
	}
		else{
			sysShowTip(sysi18n.Null_role);
		}
	
}
/*------------------------编辑层----------------------*/
//role.edit=function(){
//	role.editremoveaids=[];
//	if(role.id!=null){
//	
//	role.index2= layer.open({
//	  	  type: 1,
//	  	  title: sysi18n.Edit,
//	  	  shadeClose: true,
//	  	  move: false,
//	  	  area: ['400px', '400px'],
//	  	  content:$('#areaTreeLayer')
//	  	  
//		});
//
//	role.initChildrenTree();
//	}
//	else{
//		sysShowTip(sysi18n.Null_node);
//	}
//	
//}
role.initChildrenTree=function(){
	sysShowGlobalLoading();
	var rid=[];
	rid.push(role.rid);
	$("#areaTree").jstree("destroy");
	var url = "sys/findsysAreaByRid";
	var data = {
			"rid[]":rid,
	};
	var successCallBack = function(data) {
	$('#areaTreeScroll').perfectScrollbar();
	$("#areaTree").jstree({
		'core' : {
			'data' : data.result,
		},
		"checkbox" : {
			"keep_selected_style" : false,
		 "three_state": false,
		 "cascade":"down",
		},
		"plugins" : [ "checkbox" ],
	}).bind("loaded.jstree", function(e, data) {
		data.instance.open_all();
	});
	}
	$.sysAjax(url, data, successCallBack);
}

role.saveEdit=function(){
	var tree=$('#areaTree');
	var removeaid=tree.jstree("get_checked");
	var rname=$("#rolelist li[id='"+role.rid+"']").text();
	if (removeaid.length!=0) {
		sysShowGlobalLoading();
		var url = "sys/editRoleToArea";
		var data = {
			"rid" : role.rid,
			"aids[]" : removeaid,
			"rname":rname,
		};
		var successCallBack = function(data) {
			if (data.result) {
				if("isAdmin"!=data.result){
					sysShowSuccessTip(sysi18n.Delete_success);
					if (removeaid != null) {
						tree.jstree("hide_node", removeaid);
						tree.jstree("uncheck_all");
					// tree.jstree("delete_node",removeaid);????????
					}
				}
				else{
					sysShowTip(sysi18n.isAdmin);
				}
				

			} else {
				sysShowTip(sysi18n.Save_failed);
			}
		};
		$.sysAjax(url, data, successCallBack);
	}
	else{
		sysShowTip(sysi18n.Null_node);
	}
}
role.closeEdit=function(){
	layer.close(role.index2);
}